package com.gitee.zou8944.ipersist.test

import com.gitee.zou8944.ipersist.core.DefaultSqlSessionFactoryBuilder
import com.gitee.zou8944.ipersist.core.SqlSession
import com.gitee.zou8944.ipersist.test.mapper.UserMapper
import com.gitee.zou8944.ipersist.test.pojo.User
import org.junit.Before
import org.junit.Test

class TestUserCRUD {

    lateinit var sqlSession: SqlSession
    lateinit var userMapper: UserMapper

    @Before
    fun init() {
        val sqlSessionFactory = DefaultSqlSessionFactoryBuilder().build("sqlMapConfig.xml")
        sqlSession = sqlSessionFactory.openSession()
        userMapper = sqlSession.getMapper(UserMapper::class.java)
    }

    @Test
    fun testSelectOne() {
        val user = User().apply {
            id = 1
        }
        sqlSession.selectOne<User>("com.gitee.zou8944.ipersist.test.mapper.UserMapper.findUserById", user)
            .apply {
                println(this)
            }
    }

    @Test
    fun testSelectOneWithMapper() {
        val user = User().apply {
            id = 1
        }
        userMapper.findUserById(user).apply {
            println(this)
        }
    }

    @Test
    fun testFindAll() {
        userMapper.findAll().forEach(::println)
    }

    @Test
    fun testInsert() {
        val user = User().apply {
            name = "helloworld"
            portrait = "https://heiheihei.jpg"
        }
        val insertResult = userMapper.insertOne(user)
        println(insertResult)
    }

    @Test
    fun testUpdate() {
        val user = User().apply {
            id = 8
            name = "dami"
            portrait = "http://new_portrait.png"
        }
        val updateResult = userMapper.updateById(user)
        println(updateResult)
    }

    @Test
    fun testDelete() {
        val user = User().apply {
            id = 8
        }
        val deleteResult = userMapper.deleteById(user)
        println(deleteResult)
    }

    @Test
    fun destroy() {
        sqlSession.close()
    }

}